package com.mingtu.jgspatrol.activity;

import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.mingtu.common.adapter.MyFragmentPagerAdapter;
import com.mingtu.common.entity.TabEntity;
import com.mingtu.common.utils.Constant;
import com.mingtu.common.viewpager.LazyViewPager.LazyFragmentPagerAdapter;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.base.MyBaseActivity;
import com.mingtu.jgspatrol.databinding.ActivityResultStatisticsBinding;
import com.mingtu.jgspatrol.fragment.ReportDataFragment;
import com.mingtu.jgspatrol.fragment.TrackDataFragment;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

public class ResultStatisticsActivity extends MyBaseActivity<ActivityResultStatisticsBinding> {
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private final String[] mTitles = {"巡护数据", "上报数据"};
    private int navPosition = 0;
    private TrackDataFragment trackDataFragment;
    private ReportDataFragment reportDataFragment;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_result_statistics;
    }

    @Override
    protected void initView() {
        super.initView();
        setModuleTitle("实时统计");
    }

    @Override
    protected void initData() {

        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }

        trackDataFragment = TrackDataFragment.getInstance();
        reportDataFragment = ReportDataFragment.getInstance();
        mFragments.add(trackDataFragment);
        mFragments.add(reportDataFragment);

        binding.viewPager.setOffscreenPageLimit(mFragments.size());//需要设置此值
        binding.viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), mFragments));
        binding.viewPager.setCurrentItem(0);
        binding.viewPager.addOnPageChangeListener(onPageChangeListener);

        binding.commonTab.setTextBold(1);
        binding.commonTab.setTabData(mTabEntities);
        binding.commonTab.setOnTabSelectListener(new MyOnTabSelectListener());
        initListener();

    }

    @Override
    protected void getData() {

    }


    private void initListener() {
        initPullRefresh();//刷新
    }

    private void initPullRefresh() {
        binding.srlUp.setEnableLoadMore(false);
        binding.srlUp.setEnableOverScrollDrag(true);
        binding.srlUp.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                binding.srlUp.finishRefresh(Constant.REFRESH_TIME);
                binding.srlUp.setNoMoreData(false);
                if (navPosition == 0) {
                    trackDataFragment.refresh();
                } else if (navPosition == 1) {
                    reportDataFragment.refresh();
                }
            }
        });
    }

    private class MyPagerAdapter extends LazyFragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }


        @Override
        protected Fragment getItem(ViewGroup viewGroup, int position) {
            return mFragments.get(position);
        }
    }


    //点击tab 监听
    private class MyOnTabSelectListener implements OnTabSelectListener {

        @Override
        public void onTabSelect(int position) {
            binding.viewPager.setCurrentItem(position);
            navPosition = position;
        }

        @Override
        public void onTabReselect(int position) {

        }
    }


    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            binding.commonTab.setCurrentTab(position);
            navPosition = position;
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };


}